﻿using System;
using System.Collections;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Navigation;
using Microsoft.SharePoint.Publishing.Navigation;
using Microsoft.SharePoint.WebControls;
using SPAdaptiveMenu.Controls;
using Menu = SPAdaptiveMenu.Controls.Menu;
using MenuItemCollection = SPAdaptiveMenu.Controls.MenuItemCollection;

namespace SPAdaptiveMenu.WebPartExample.VisualDemoWebPart
{
    public partial class VisualDemoWebPartUserControl : UserControl
    {
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            var simpleMenu = new Menu
                                 {
                                     Title = "Simple menu",
                                     IconSize = MenuIconSize.Small,
                                     Width = new Unit("250px")
                                 };
            var c = new SPSiteMapProvider();
            var r = c.RootNode;
            simpleMenu.Title = r.Title;
            simpleMenu.DataSource = r.ChildNodes;
            simpleMenu.DataBind();
            Controls.Add(simpleMenu);
            var site = SPContext.Current.Site;
            var rootWeb = site.RootWeb;
            var menu = new Menu
            {
                IconSize = MenuIconSize.Small,
                Title = rootWeb.Title,
                Width = new Unit("250px")
            };
            FillWebMenuItems(rootWeb, menu.ItemCollection);
            Controls.Add(menu);
        }

        private void FillWebMenuItems(SPWeb web, MenuItemCollection menuItems)
        {
            var lists = web.Lists.Cast<SPList>().Where(l => !l.Hidden);
            var webs = web.Webs;
            if (lists.Count() > 0)
            {
                var menuLists = new MenuNested
                                    {
                                        Title = "Lists and Libraries"
                                    };
                menuItems.Add(menuLists);
                foreach (SPList list in lists)
                {
                    menuLists.ItemCollection.Add(new MenuLink
                                                     {
                                                         Title = list.Title + " (" + list.ItemCount + ")",
                                                         Url = list.DefaultViewUrl,
                                                         IconSrc = list.ImageUrl
                                                     });
                }
            }
            foreach (SPWeb subWeb in webs)
            {
                var subMenu = new MenuNested { Title = subWeb.Title, IconSrc = "/_layouts/images/stsicon.gif" };
                menuItems.Add(subMenu);
                FillWebMenuItems(subWeb, subMenu.ItemCollection);
            }
        }

        protected void OnCommandAction(object sender, EventArgs e)
        {
            var arg = e as MenuItemEventArgs;
            var item = sender as MenuPostBack;
            if (item != null)
            {
                item.CommandArgument += DateTime.Now.Second.ToString() + " ";
                item.Title += "+";
            }
            var wp = Parent as WebPart;
            if (wp != null && arg != null)
            {
                wp.Title = arg.Argument;
            }
            else
            {
                wp.Title = "Unknown user or empty argument";
            }
        }
    }
}
